$(function () {
    //获取url
    var url = localStorage.getItem("ajaxUrl");
    var pass = 1;
    var warehouse = "";

    //点击返回按钮
    $(".head").on("click", ".goBack", function () {
        $.confirm("确定返回吗？", function () {
            localStorage.removeItem("work");
            window.location.href = "pt_cgrk.html";
        }, function () {
            $("#goods_id").focus();
        });
    });

    //搜索框 获取焦点
    $("#goods_id").focus();

    //点击 显示 隐藏
    $(".head .icon-xianshi").on("click", function () {
        $(this).hide();
        $(".head .icon-yincang").show();
        $(".details").show();
        //$alert("显示成功");
        setTimeout(function () {
            $("#goods_id").val("").focus();
        }, 1400);
    });
    $(".head .icon-yincang").on("click", function () {
        $(this).hide();
        $(".head .icon-xianshi").show();
        $(".details").hide();
        //$alert("隐藏成功");
        setTimeout(function () {
            $("#goods_id").val("").focus();
        }, 1400);
    });

    var work = JSON.parse(localStorage.getItem("work"));
    $(".details span.good_jsck").html(work.ware_name.trim());
    $(".details span.good_name").html(work.inv_name.trim());
    $(".details span.good_zl").html(work.actual_net_weight);
    $(".details span.good_js").html(work.piece_excuted);

    //根据扫描内容 得到 产品信息
    $("#goods_id").on("keydown", function (event) {
        if (event.keyCode == "13") {
            var good_length = $(".details span.good_length").html();
            var good_js = $(".details span.good_js").html();
            if (Number(good_length) >= Number(good_js)) {
                $alert("扫描完成，请点击确认！");
                return;
            }
            if (pass == 0) {
                return
            }
            pass = 0;
            var val = $(this).val();
            //$("#goods_id").blur();
            if (!val) {
                $alert("请扫描盘条标签！");
                setTimeout(function () {
                    pass = 1;
                    $("#goods_id").val("").focus();
                }, 1400);
                return;
            }
            $(this).val("");
            loading_show("获取中...");
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "post",
                url: url + '/app/wirerodpurchaseinorder/getQrcodeMessage',
                data: {
                    _id: work.id,
                    serial: val
                },
                dataType: 'json',
                json: 'callback',
                cache: false,
                success: function (res) {
                    pass = 1;
                    loading_hide();
                    if (res.success) {
                        var data = res.result;
                        if ($(".content .list-block .data_ul li[attr-data='" + (data.batch ? data.batch : data.number) + "']").length != 0) {
                            $alert("不可重复扫描");
                            setTimeout(function () {
                                $("#goods_id").val("").focus();
                            }, 1400);
                        } else {
                            $(".mengceng").show();
                            $("div.div_pt").show();
                            $("div.div_pt .cpph").val(data.number);
                            $("div.div_pt .cplh").val(data.stove);
                            $("div.div_pt .cpjh").val(data.volume);
                            $("div.div_pt .cpzl").val(data.weight).focus();
                            $("div.div_pt .cpkw").val(data.position);
                            $("div.div_pt .describe").val(val);
                            warehouse = data;
                        }
                    } else {
                        setTimeout(function () {
                            $("#goods_id").val("").focus();
                        }, 1400);
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    pass = 1;
                    loading_hide();
                    console.log(error);
                    setTimeout(function () {
                        $("#goods_id").val("").focus();
                    }, 1400);
                    $alert(error.responseText);
                }
            });
        }
    });

    $("div .pt_quxiao").on("click", function () {
        $(".mengceng").hide();
        $("div.div_pt").hide();
        $("#goods_id").val("").focus();
    });

    //库位扫描
    $("div.div_pt input.cpkw").on("keyup", function (event) {
        if (event.keyCode == 13) {
            if (!$("div.div_pt input.cpkw").val()) {
                return $alert("库位不可为空");
            }
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "post",
                url: url + '/app/wirerodpurchaseinorder/getPositionByhouse',
                data: {
                    invCode: work.inv_code,
                    houseCode: work.ware_code,
                    positionCode: $("div.div_pt input.cpkw").val()
                },
                dataType: 'json',
                json: 'callback',
                cache: false,
                success: function (res) {
                    if (res.success) {
                        var data = res.result;
                        $("div.div_pt input.cpkw").val(data ? data : "");
                    } else {
                        setTimeout(function () {
                            $("div.div_pt input.cpkw").val("").focus();
                        }, 1400);
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    console.log(error);
                    setTimeout(function () {
                        $("div.div_pt input.cpkw").val("").focus();
                    }, 1400);
                    $alert(error.responseText);
                }
            });
        }
    });

    $("div.div_pt .pt_queding").click(function () {
        if (pass == 0) {
            return;
        }
        pass = 0;
        var cpph = $("div.div_pt input.cpph").val();
        var cplh = $("div.div_pt input.cplh").val();
        var cpjh = $("div.div_pt input.cpjh").val();
        var cpzl = $("div.div_pt input.cpzl").val();
        var cpkw = $("div.div_pt input.cpkw").val();
        var describe = $("div.div_pt input.describe").val();
        if (!cpzl || cpzl < 1) {
            pass = 1;
            $alert("重量不可为空");
        } else if (!cplh) {
            pass = 1;
            $alert("炉号不可为空");
        } else if (!cpjh) {
            pass = 1;
            $alert("卷号不可为空");
        } else if (!cpkw) {
            pass = 1;
            $alert("库位不可为空");
        }
        else {
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "post",
                url: url + '/app/wirerodpurchaseinorder/getPositionByhouse',
                data: {
                    invCode: work.inv_code,
                    houseCode: work.ware_code,
                    positionCode: cpkw
                },
                dataType: 'json',
                json: 'callback',
                cache: false,
                success: function (res) {
                    if (res.success) {
                        pass = 1;
                        // var data = res.result;
                        var li_con = '<li attr-data="' + cpph + '"><div class="row no-gutter"><div class="col-90">' +
                            '<p><span>编号：</span><span class="sp pihao">' + cpph + '</span></p>' +
                            '<p><span>重量：</span><span class="sp dhsl">' + cpzl + '</span></p>' +
                            '<p><span>炉号：</span><span class="sp lh">' + cplh + '</span>&nbsp;&nbsp;' +
                            '<span>卷号：</span><span class="sp jh">' + cpjh + '</span></p>' +
                            '<p><span>库位：</span><span class="sp kw">' + cpkw + '</span></p>' +
                            '<p class="p_none"><span>describe：</span><span class="sp describe">' + describe + '</span></p>' +
                            '</div><div class="col-10"><span class="icon iconfont icon-msnui-jian"></span>';
                        if (cpph.indexOf("PT") == 0) {
                            li_con += '<span class="icon iconfont icon-dayin" style="margin-top: 2rem"></span>';
                        }
                        li_con += '</div></div></li>';
                        $(".content .data_ul").prepend(li_con);

                        badge_text();
                        $alert("保存成功", 800);
                        $(".mengceng").hide();
                        $("div.div_pt").hide();
                        $("#goods_id").val("").focus();
                        if (cpph.indexOf("PT") == 0) {
                            cordova.plugins.print.wirerodpurchaseinorder(cpph, "澳森特钢", (work.inv_spec), cpzl, (work.inv_name), function (result) {
                            }, function error(error) {
                            });
                        }
                    } else {
                        pass = 1;
                        console.log(res);
                        setTimeout(function () {
                            $("div.div_pt input.cpkw").val("").focus();
                        }, 1400);
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    pass = 1;
                    console.log(error);
                    setTimeout(function () {
                        $("div.div_pt input.cpkw").val("").focus();
                    }, 1400);
                    $alert(error.responseText);
                }
            });
        }
    });

    // li 中的 减号 点击 删除 本条
    $(".content").on("click", ".data_ul li .col-10 .icon-msnui-jian", function () {
        var jianhao = $(this);
        $.confirm('确定删除此货品？', function () {
            jianhao.parent().parent().parent().remove();
            badge_text();
            $alert("删除成功");
            $("#goods_id").val("").focus();
        });
    });

    // li 中的 打印
    $(".content .data_ul").on("click", '.icon-dayin', function () {
        var li = $(this).parent().parent().parent();
        var cpph = li.find(".pihao").text();
        var cpzl = li.find(".dhsl").text();
        $alert(cpph + "打印成功");
        cordova.plugins.print.wirerodpurchaseinorder(cpph, "澳森特钢", (work.inv_spec), cpzl, (work.inv_name), function (result) {
        }, function error(error) {
        });
    });

    //点击 获取编号
    $(".foot").on("click", ".add_go", function () {
        if (pass == 0) {
            return
        }
        pass = 0;
        $.ajax({
            headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
            type: "post",
            url: url + '/app/wirerodpurchaseinorder/getPtNumber',
            data: {
                _id: work.id,
                invCode: work.inv_code,
                houseCode: work.ware_code
            },
            dataType: 'json',
            json: 'callback',
            cache: false,
            success: function (res) {
                pass = 1;
                if (res.success) {
                    var data = res.result;
                    $(".mengceng").show();
                    $("div.div_pt").show();
                    $("div.div_pt input.cpph").val(data.number);
                    //$("div.div_pt input.cplh").val("");
                    $("div.div_pt input.cpjh").val("");
                    $("div.div_pt input.cpzl").val(data.weight);
                    $("div.div_pt input.cpkw").val(data.position).focus();
                    $("div.div_pt input.describe").val(data.number);
                } else {
                    $alert(res.message);
                }
            },
            error: function (error) {
                pass = 1;
                console.log(error);
                setTimeout(function () {
                    $("#goods_id").val("").focus();
                }, 1400);
                $alert(error.responseText);
            }
        });
    });

    // 点击 添加出库 信息 传给后台
    $(document).on("click", ".foot .add_success", function () {
        if ($(".content .data_ul li").length <= 0) {
            $("#goods_id").val("").focus();
            return $alert("请添加入库的盘条！");
        }
        $.confirm('共' + $(".content .data_ul li").length + '件，确定提交？', function () {
            if (pass == 0) {
                return
            }
            pass = 0;
            var datas = [];
            for (var j = 0; j < $(".content .data_ul li").length; j++) {
                var pihao = $(".content .data_ul li").eq(j).find("span.pihao").text();
                var dhsl = $(".content .data_ul li").eq(j).find("span.dhsl").text();
                var lh = $(".content .data_ul li").eq(j).find("span.lh").text();
                var jh = $(".content .data_ul li").eq(j).find("span.jh").text();
                var kw = $(".content .data_ul li").eq(j).find("span.kw").text();
                var describe = $(".content .data_ul li").eq(j).find("span.describe").text();
                datas.push({
                    batch: pihao,
                    position: kw,
                    describe: describe,
                    stove: lh,
                    column: jh || "",
                    weight: Number(dhsl),
                });
            }
            loading_show("提交中...");
            //获取 货品列表中的信息
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "POST",
                url: url + "/app/wirerodpurchaseinorder/saveWirerodin",
                data: {
                    _id: work.id,
                    datas: JSON.stringify(datas)
                },
                timeout: 60000,
                cache: false,
                success: function (res) {
                    pass = 1;
                    if (res.success) {
                        loading_hide();
                        console.log("盘条入库成功");
                        localStorage.removeItem("work");
                        window.location.href = "pt_cgrk.html";
                    } else {
                        loading_hide();
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    pass = 1;
                    if (error.status == 0) {
                        loading_hide();
                        $alert("请求超时，请重新操作！");
                    } else {
                        loading_hide();
                        $alert(error.responseText);
                    }
                }
            });
        })
    });

    //点击 某一条 数据  改变 背景颜色
    $(document).on("click", ".content .data_ul li", function () {
        $(this).css("background", "#cccccc").siblings("li").css("background", "#ffffff");
    });

    // goodsList中 badge 的值 为 列表中 li 的个数
    function badge_text() {
        var badge_len = $(".content .data_ul li").length;
        //console.log(badge_len);
        var allWeight = 0;
        for (var i = 0; i < badge_len; i++) {
            var weight = $(".content .data_ul li").eq(i).find(".row span.dhsl").html();
            allWeight = accAdd(allWeight, weight);
        }
        $(".details span.good_length").html(badge_len);
        $(".details span.good_weight").html(allWeight);
    }

    badge_text();

});